﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.Types;
using 医院预约Web.Models;


namespace 医院预约Web.Controllers
{
    public class VipController :StopperController
    {
        public IActionResult ba2d0cf27b323a48()//科室选择
        {
            string account = HttpContext.Session.GetString("name");
            ViewData["account"]=account;
            DataBase context = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            return View(context.GetClassdbos());
        }

        public IActionResult  ae99fee77bffd211851b7d()//管理员界面TLC
        {
            DataBase context = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            return View(context.GetAllAccount());
        }

        public IActionResult aafd48425d67bcb0()//修改班级界面
        {
            ViewData["account"] = Request.Query["account"];//Http Request
            return View();
        }
        [HttpPost]
        public void aafd48425d67bcb0(string account,string type)//修改函数TLC
        {
            if(String.IsNullOrEmpty(type)) Response.Redirect(Url.Action("ae99fee77bffd211851b7d", "Vip"));
            DataBase db = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            db.update(account, type);
            Response.Redirect(Url.Action("ae99fee77bffd211851b7d", "Vip"));
        }
        
        public void bd31e0ea9(string account)//删除函数TLC
        {
            DataBase db = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            db.delete(account);
            Response.Redirect(Url.Action("ae99fee77bffd211851b7d", "Vip"));
        }

        public IActionResult DoctorChoose(int classnum)//医生选择
        {
            string kesi=null;
            switch(classnum)
            {
                case 1:
                    kesi = "外科";
                    break;
                case 2:
                    kesi = "内科";
                    break;
                case 3:
                    kesi = "儿科";
                    break;
                case 4:
                    kesi = "骨科";
                    break;
            }
            ViewData["科室"] = kesi;
            DataBase context = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            return View(context.GetDoctordbo(classnum));
        }

        public IActionResult MyUpdate(int id, string doctorname,string createtime,string username,int userage)//修改預約
        {
            ViewData["username"] = username;
            ViewData["userage"] = userage;
            ViewData["doctorname"] = doctorname;
            ViewData["createtime"] = createtime;
            int i;
            DataBase dbfunction = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            List<Models.Appointmentlist> list = new List<Models.Appointmentlist>();//定制类型方便输出
            for (i = 1; i <= 6; i++)
            {
                list.Add(new Models.Appointmentlist()//DateTime.Now：返回当前时间，精确到秒 DateTime.Today：返回当前时间，精确到天
                {
                    dateTime = DateTime.Today.AddDays(i).ToShortDateString(),//获得今日之后（Now.AddDays)第i日的日期
                    aid = id,
                    appointment = dbfunction.GetWeekdbo(id, Convert.ToInt16(DateTime.Now.DayOfWeek) + i, DateTime.Today.AddDays(i).ToShortDateString(), i),//这里获得WEEK的1~7数字表示，加上未来1~7日所以在DB函数中的switch要考虑到week值取值为1~14                  
                });
            }
            return View(list);
        }
        public IActionResult Appointment(int id,string doctorname,int classnumber)//预约数据处理
        {
            List<Models.Appointmentlist> list = new List<Models.Appointmentlist>();//定制类型方便输出
            ViewData["doctorname"] = doctorname;
            ViewData["classnumber"] = classnumber;
            ViewData["Appointmentable"] = "";
            string account = HttpContext.Session.GetString("name");
            DataBase dbfunction = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            if (dbfunction.Appointmentornot(id, account)) ViewData["Appointmentable"] = "Yes";//排重复预约
            else return View();
            int i;
            for (i=1; i <=6; i++)
            {
                list.Add(new Models.Appointmentlist()//DateTime.Now：返回当前时间，精确到秒 DateTime.Today：返回当前时间，精确到天
                {
                    dateTime = DateTime.Today.AddDays(i).ToShortDateString(),//获得今日之后（Now.AddDays)第i日的日期
                    aid = id,
                    appointment= dbfunction.GetWeekdbo(id, Convert.ToInt16(DateTime.Now.DayOfWeek)+ i , DateTime.Today.AddDays(i).ToShortDateString(),i),//这里获得WEEK的1~7数字表示，加上未来1~7日所以在DB函数中的switch要考虑到week值取值为1~14                  
                });
            }               
            return View(list);
        }
        public IActionResult Appointment2(string aid, string date, string doctorname,string appointment)
        {
            
            ViewBag.id = aid;
            ViewBag.date = date;
            ViewBag.doctorname = doctorname;
            ViewData["appointment"] = appointment;
            return View();
        }
        public void MyCancel(string createtime)//取消預約
        {
            DataBase dbfunction = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            if (dbfunction.mycancel(createtime)) Response.Redirect(Url.Action("My", "Vip"));
        }
        public IActionResult Formpost(int aid,string date,int appointment,string doctorname)//预约数据处理
        {string account = HttpContext.Session.GetString("name");
            ViewData["doctorname"] = doctorname;
            ViewData["id"] = aid;
            ViewData["date"] = date;
            ViewData["appointment"] = appointment;
            DataBase dbfunction = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            ViewData["timeconfirm"] = dbfunction.timeconfirm(account, date, appointment);//	同一时段，不能安排多个预约就诊
            return View();
        }
        public IActionResult FormpostUpdate(int aid, string date, int time, string doctorname, string createtime,string username,int userage)//预约数据处理
        {
            ViewData["doctorname"] = doctorname;
            ViewData["id"] = aid;
            ViewData["date"] = date;
            ViewData["time"] = time;
            ViewData["createtime"] = createtime;
            ViewData["username"] = username;
            ViewData["userage"] = userage;
            return View();
        }
        [HttpPost]
        public void FormpostUpdate( string date,string createtime,int time)//预约数据处理
        {         
            DataBase dbfunction = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;          
            if (dbfunction.frompostingupdate(date,time,createtime))
                Response.Redirect(Url.Action("Success", "Vip"));
            else Response.Redirect(Url.Action("LoginError", "Home"));
        }
        [HttpPost]
        public void Formpost(int id, string date, int time, string name, int age)//预约数据处理
        {
            string account = HttpContext.Session.GetString("name");
            DataBase dbfunction = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            int kesi=0;
            if (1000 < id && id < 1999) kesi = 1;
            else if (2000 < id && id < 2999) kesi = 2;
            else if (3000 < id && id < 3999) kesi = 3;
            else if (4000 < id && id < 4999) kesi = 4;
            else if (5000 < id && id < 5999) kesi = 5;
            if (dbfunction.fromposting(id, date, time, name, age, account,kesi))
                Response.Redirect(Url.Action("Success", "Vip"));
            else Response.Redirect(Url.Action("LoginError", "Home"));
        }

        public IActionResult Success()
        {
            return View();
        }

        public IActionResult My()//我的预约
        {
            DataBase dbfunction = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            string account = HttpContext.Session.GetString("name");
            ViewData["account"] = account;
            return View(dbfunction.GetMylist(account));
        }

        public IActionResult MyDetial(string username,int userage,string createtime,string classname,string classplace ,string date,string doctorname,int time,int status,int id,string tell)
        {
            string nowstr = DateTime.Today.ToShortDateString();
            DateTime Date_NowDateTime = Convert.ToDateTime(nowstr);
            DateTime Date_date = Convert.ToDateTime(date);
            if (DateTime.Compare(Date_NowDateTime, Date_date) == 0 || DateTime.Compare(Date_NowDateTime, Date_date) > 0) //今天=预约时间||今天<预约时间
                ViewData["invalid"] = 1;
            else ViewData["invalid"] = 0;
           // return Date_NowDateTime.ToString() + "\n" +date+ DateTime.Compare(Date_NowDateTime, Date_date).ToString();
            ViewData["status"] = status;
            ViewData["username"] = username;
            ViewData["userage"] = userage;
            ViewData["createtime"] = createtime;
            ViewData["classname"] = classname;
            ViewData["classplace"] = classplace;
            ViewData["date"] = date;
            ViewData["doctorname"] = doctorname;
            ViewData["id"] = id;
            ViewData["time"] = time;
            ViewData["tell"] = tell;
            return View();
        }
    }
}